package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class StatementServiceImpl implements IStatementService {
    @Resource
    private StatementMapper statementMapper;
    @Resource
    private StatementItemMapper statementItemMapper;
    @Resource
    private IAppointmentService appointmentService;

    @Override
    public Statement queryById(Long id) {
        return statementMapper.queryById(id);
    }

    @Override
    public TablePageInfo<Statement> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(statementMapper.queryForList(qo));
    }

    @Override
    @Transactional
    public void save(Statement statement) {
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    @Transactional
    public void update(Statement statement) {
        Statement oldObj = queryById(statement.getId());
        if (Statement.STATUS_PAID.equals(oldObj.getStatus()))
            throw new BusinessException("只有消费中的预约单才能编辑");
        statementMapper.update(statement);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        statementMapper.deleteById(id);
        statementItemMapper.deleteByStatementId(id);
    }

    @Override
    public void updateAmount(BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal itemPrice, Long statementId) {
        statementMapper.updateAmount(totalAmount, totalQuantity, itemPrice, statementId);
    }

    @Override
    public void payStatement(Long id) {
        // 结算单
        Statement statement = statementMapper.queryById(id);
        // 合理性判断
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new BusinessException("处于消费中才能进行支付");
        }
        // 更新状态
        statementMapper.status(id, Statement.STATUS_PAID, ShiroUtils.getUserId());
        Long appointmentId = statement.getAppointmentId();
        if (appointmentId != null) {
            // 更新预约单状态
            appointmentService.changStatus(appointmentId, Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public Statement queryAppointmentId(Long appointmentId) {
        return statementMapper.queryAppointmentId(appointmentId);
    }
}
